package com.taptech.doufu.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.TMAnalysis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longsiyang on 16/4/8.
 */
public class HomeBannerImageAdapter extends BaseAdapter
{
    private List<DFHomeBannerBean> bannerList;
    private Context mContext;
    private LayoutInflater mInflater;
    private String homePageType;//首页分类

    public HomeBannerImageAdapter(List<DFHomeBannerBean> bannerList, Context mContext,String homePageType)
    {
        this.bannerList = bannerList;
        this.mContext = mContext;
        this.homePageType = homePageType;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);// 此处注意，保持文字一直存在
    }

    @Override
    public int getCount()
    {
        if (bannerList != null && bannerList.size() == 1) {
            return bannerList.size();
        }else{
            return Integer.MAX_VALUE;// 无限循环
        }
    }

    public void setData(List<DFHomeBannerBean> list)
    {
        this.bannerList = list;
    }

    @Override
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return bannerList.get(position % bannerList.size());
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.pager_news_channel_list_header, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.iv_pager_main_tab_news_header_banner);
            holder.titile = (TextView) convertView.findViewById(R.id.tv_pager_main_tab_news_header_title);
            holder.rlAD = (RelativeLayout) convertView.findViewById(R.id.rlAD);
            holder.ivIconAD = (ImageView) convertView.findViewById(R.id.ivIconAD);
            convertView.setTag(holder);
        } else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        changeImageAlpha(holder.imageView);
        if (bannerList == null || (bannerList != null && bannerList.size() == 0))
            return convertView;
        if (position % bannerList.size() >= 0 && position % bannerList.size() < bannerList.size()) {
            DFHomeBannerBean bean = bannerList.get(position % bannerList.size());
            if (bean.isIs_ad()) {
                holder.rlAD.setVisibility(View.VISIBLE);
                holder.ivIconAD.setVisibility(View.VISIBLE);
                //显示广告
            }else{
                holder.ivIconAD.setVisibility(View.GONE);
                holder.rlAD.setVisibility(View.GONE);
                showGovernmentBanner(holder,bean,convertView,position);
            }
        }
        return convertView;
    }


    /**
     * 显示官方轮播图
     * @param holder
     * @param bean
     * @param convertView
     * @param position
     */
    private void showGovernmentBanner(ViewHolder holder,DFHomeBannerBean bean,View convertView,final int position) {
        holder.titile.setText(bean.getTitle());
        if (bean.getImages() != null) {
            GlideUtil.displayImage(holder.imageView,bannerList.get(position % bannerList.size()).getImages()[0].getImgUrl(),R.drawable.img_default_loading_banner);
        } else if (!TextUtils.isEmpty(bean.getBanner_img())) {
            GlideUtil.displayImage(holder.imageView,bean.getBanner_img(),R.drawable.img_default_loading_banner);
        }


        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                if ("1".equals(((WeMediaApplication) (WeMediaApplication.applicationContext)).sexType)) {//彩虹
                    TMAnalysis.event(mContext, "3_8_DM_novelhome_Carouselfigure"+(position+1));
                } else {//非彩虹
                    TMAnalysis.event(mContext, "3_8_FD_novelhome_Carouselfigure"+(position+1));
                }
                // TODO Auto-generated method stub
                DFHomeBannerBean bean = bannerList.get(position % bannerList.size());

                if (Constant.OBJECT_TYPE_NOVEL == DiaobaoUtil.String2Int(bean.getObject_type())
                        || Constant.CONTENT_TYPE_TOPIC == DiaobaoUtil.String2Int(bean.getObject_type())){
                    String novelID = "";
                    if (bean != null) {
                        novelID = bean.getId();
                    }
                    UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_NOVEL_DETAIL,novelID,"轮播图",homePageType,"TFNovelDetail.js");
                }
                // 轮播图统计事件
                Map<String, String> analysisMap = new HashMap<String, String>();
                analysisMap.put("position", position % bannerList.size() + "");
                analysisMap.put("id", bean.getId());
                analysisMap.put("object_type", bean.getObject_type());
                analysisMap.put("page_type",homePageType);
                TMAnalysis.event(mContext, UmengEventName.HOME_BANNER, analysisMap);
                StartActivityUtil.bannerOnclick(mContext,bean);
            }
        });
    }

    public static class ViewHolder
    {
        ImageView imageView;
        TextView titile;
        RelativeLayout rlAD;
        ImageView ivIconAD;
    }


    private void changeImageAlpha(ImageView imageView){
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            imageView.setBackgroundColor(WeMediaApplication.getInstance().getResources().getColor(R.color.black));
            imageView.setAlpha(0.8f);
        }else {
            imageView.setAlpha(1.0f);
        }
    }
}
